package com.fulihui.wgj.service.uv;

import com.alibaba.dubbo.config.annotation.Service;
import com.fulihui.common.builder.rpc.RpcResultBuilder;
import com.fulihui.common.error.Errors;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.util.DateUtils;
import com.fulihui.wgj.config.properties.CommonProps;
import com.fulihui.wgj.dal.impala.mapper.WxUserMarkInfoMapper;
import com.fulihui.wgj.facade.service.uv.WxUserMarkInfoService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 微信用户点击标识-统计uv
 *
 * @author wang_wx
 * @date 2018-08-09
 */
@Component
@Service(interfaceClass = WxUserMarkInfoService.class, version = "1.0.0", timeout = 6000)
public class WxUserMarkInfoServiceImpl implements WxUserMarkInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxUserMarkInfoServiceImpl.class);

    /**
     * 队列一般长度
     */
    private static final int mqLengthGenerally = 200;

    @Resource
    WxUserMarkInfoMapper wxUserMarkInfoMapper;

    @Autowired
    CommonProps commonProps;

    @Autowired
    RestTemplate restTemplate;

    @Override
    public BaseResult countUvAndStorage() {
        // 昨天的时间
        long yesterdayBegin;
        long yesterdayEnd;
        try {
            String yesterday1 = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MIN).format(DateTimeFormatter.ofPattern(DateUtils.newFormat));
            yesterdayBegin = DateUtils.parseNewFormat(yesterday1).getTime();
            String yesterday2 = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MAX).format(DateTimeFormatter.ofPattern(DateUtils.newFormat));
            yesterdayEnd = DateUtils.parseNewFormat(yesterday2).getTime();
        } catch (ParseException e) {
            LOGGER.error("countUvAndStorage parseNewFormat error" + e);
            return RpcResultBuilder.fail(Errors.Commons.SYSTEM_ERROR);
        }
        List<Map<String, Object>> list = wxUserMarkInfoMapper.countUv(yesterdayBegin, yesterdayEnd);
        LOGGER.info("countUvAndStorage list = {}", list);
        if (!CollectionUtils.isEmpty(list)) {
            List<List<Map<String, Object>>> partition = Lists.partition(list, mqLengthGenerally);
            partition.stream().forEach(maps -> {
                String response = restTemplate.postForObject(commonProps.getShortLinkHost() + "/api/storageUv", list, String.class);
                LOGGER.info("调用短链接进行uv入库响应：response={}", response);
            });
        }
        return RpcResultBuilder.succ();
    }
}
